/**
 * Created by zhumingming on 2017/6/17.
 */
(function (window) {

    var $ = window.$ || window.JQuery;


    if (!$) {
        throw new Error("jquery  未定义！");
    }

    var oldValue = "";

    var onChangeClock;

    function bE(element) {

        this.target = $(element);
        this.target.attr("contenteditable", true);
        var $this = this;


        this.target.bind('blur keyup paste copy cut mouseup', function () {
            var htmlnew = $(this).html();
            if (oldValue !== htmlnew) {
                $this.newValue = htmlnew;
                oldValue = htmlnew;
                if (onChangeClock) {
                    clearTimeout(onChangeClock);
                }
                onChangeClock = setTimeout(function () {
                    $this.onChange ? $this.onChange(htmlnew) : null;
                }, 500)
            }
        })

    }


    var extend = {
        //删除格式化
        clearFormate: function () {
            document.execCommand("removeFormat", false, null);
        },
        //插入文本
        insertText: function (text) {
            document.execCommand("insertText", false, text);
        },
        head: function (tag/*h1-h6-p*/) {
            document.execCommand("heading", false, tag);
        },
        insertHtml: function (html) {
            document.execCommand("insertHTML", false, html);
        },
        setFontColor: function (color) {
            document.execCommand("foreColor", false, color);
        },
        link: function (url) {
            if (!url)
                return;
            document.execCommand("createLink", false, this.util.safeStr(url));
        },
        unLink: function () {
            document.execCommand("unlink", false, null);
        },
        //创建换行符
        pastDue: function () {
            document.execCommand("insertHorizontalRule", false, null);
        },
        //创建下划线
        underLine: function () {
            document.execCommand("underline", false, null);
        },
        //创建过期线
        underLine: function () {
            document.execCommand("strikeThrough", false, null);
        },
        //重做
        redo: function () {
            document.execCommand("redo", false, null);
        },
        //回退
        unDo: function () {
            document.execCommand("undo", false, null);
        },
        //有序列表
        orderList: function () {
            document.execCommand("insertOrderedList", false, null);
        },
        //无序列表
        unOrderList: function () {
            document.execCommand("insertUnorderedList", false, null);
        },
        //左对齐
        toLeft: function () {
            document.execCommand("justifyLeft", false, null);
        },
        //右对齐
        toRight: function () {
            document.execCommand("justifyRight", false, null);
        },
        //居中对齐
        toMiddle: function () {
            document.execCommand("justifyCenter", false, null);
        },

        util: {
            safeStr: function (str) {
                return str.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;").replace("/javascript/", "");
            }
        }

    }

    function doExetend(source, target) {
        for (var key in source) {
            target[key] = source[key];
        }
    }

    doExetend(extend, bE.prototype);

    function E(element, option) {
        doExetend(option, bE.prototype);
        return new bE(element);
    }

    window.E = E;

})(window)